<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\GrSeckill;
use App\Admin\show\goods;

use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class GrSeckillController extends CouponAdmin
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GrSeckill(), function (Grid $grid) {
            $grid->column('seckill_id','ID')->sortable();
            $grid->column('seckill_name');
            $grid->column('participate_frequency','每天可参与次数');
            $grid->column('company','单位');
            $grid->column('limit_number','活动限制库存');
            $grid->column('seckill_price','秒杀价格');
            $grid->column('goods_id', '秒杀商品')->display('秒杀商品')->modal(function ($modal) {
                $modal->title('秒杀商品');
                return goods::make(['goods_id' => $this->goods_id]);
            });
            $grid->column('seckill_start_time');
            $grid->column('seckill_end_time');
            $grid->column('seckill_img_url')->image('', 100, 100);
            $grid->column('seckill_state')->radio(['关闭','开启', '抢购中']);
            $grid->column('is_free_shipping','是否包邮')->radio(['是', '否']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->quickSearch('seckill_name')->placeholder('搜索秒杀名称');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('seckill_state','秒杀状态')->select([ '关闭','开启', '抢购中']);
                $filter->equal('seckill_name','秒杀名称');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GrSeckill(), function (Show $show) {

            $show->field('seckill_id')->sortable();
            $show->field('seckill_name');
            $show->field('seckill_content');
            $show->field('participate_frequency');
            $show->field('company');
            $show->field('limit_number');
            $show->field('seckill_price');
            $show->field('goods_id', '秒杀商品')->display('积分详情')->modal(function ($modal) {
                $modal->title('秒杀商品');
                return goods::make(['goods_id' => $this->goods_id]);
            });
            $show->field('seckill_start_time');
            $show->field('seckill_end_time');
            $show->field('seckill_img_url')->image('', 100, 100);
            $show->field('seckill_state')->radio(['开启', '关闭']);
            $show->field('is_free_shipping')->radio(['开启', '关闭']);
            $show->field('created_at');
            $show->field('updated_at')->sortable();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GrSeckill(), function (Form $form) {
            $form->display('seckill_id');
            $form->text('seckill_name');
            $form->textarea('seckill_content','秒杀简介');
            $form->datetime('seckill_start_time');
            $form->datetime('seckill_end_time');
            $form->multipleSelectTable('goods_id', '请选择秒杀的商品')
                ->from(UserTable::make())->saving(function ($e) {
                    return $e[0];
                });
            $form->image('seckill_img_url')->autoUpload()->uniqueName()->saveFullUrl();
            $form->radio('is_free_shipping', '是否包邮')->options(['是', '否']);
            $form->number('participate_frequency', '用户限制次数');
            $form->text('company', '单位:（例：袋）');
            $form->text('seckill_price', '秒杀价格');
            $form->number('limit_number', '限制数量');
            $form->radio('seckill_state')->options(['开启', '关闭']);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
